package cake.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cake.bean.Order;
import cake.db.MySqlDb;
//������ݿ���ʶ���
public class OrderDao {
private MySqlDb db=new MySqlDb();
	//ͨ��id��ȡ��������
	public Order getOrder(long id)
	{
		Order order=null;
		String sql="select * from myorder where id="+id;
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				order=new Order();
				order.setAddress(rs.getString("address"));
				order.setAmount(rs.getInt("amount"));
				order.setCode(rs.getString("code"));
				order.setId(rs.getLong("id"));
				order.setPhone(rs.getString("phone"));
				order.setRemark(rs.getString("remark"));
				order.setStatus(rs.getInt("status"));
				order.setUserId(rs.getLong("userid"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			db.Close();
		}
		
		return order;
	}
	//ͨ��Ż�ȡ��������
	public Order getOrder(String code)
	{
		Order order=null;
		String sql="select * from myorder where code='"+code+"'";
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				order=new Order();
				order.setAddress(rs.getString("address"));
				order.setAmount(rs.getInt("amount"));
				order.setCode(rs.getString("code"));
				order.setId(rs.getLong("id"));
				order.setPhone(rs.getString("phone"));
				order.setRemark(rs.getString("remark"));
				order.setStatus(rs.getInt("status"));
				order.setUserId(rs.getLong("userid"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			db.Close();
		}
		
		return order;
	}
	
	//��ҳ��ȡ�����б?��pageSizeΪ0ʱ��ʾ����ҳ
	public List<Order> getOrders(int currPage,int pageSize,String where)
	{
		List<Order> orders=new ArrayList<Order>();
		String sql="select * from myorder ";
		if(!where.equals(""))
		{
			sql+=" Where "+where;
		}
		if(pageSize!=0)
		{
			sql+=" limit "+((currPage-1)*pageSize)+","+pageSize;
		}
		
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				Order order=new Order();
				order.setAddress(rs.getString("address"));
				order.setAmount(rs.getInt("amount"));
				order.setCode(rs.getString("code"));
				order.setId(rs.getLong("id"));
				order.setPhone(rs.getString("phone"));
				order.setRemark(rs.getString("remark"));
				order.setStatus(rs.getInt("status"));
				order.setUserId(rs.getLong("userid"));
				
				orders.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		
		return orders;
	}
	//��������
	public boolean addOrder(Order order)
	{
		order.setStatus(0);
		Date d=new Date();
		DateFormat format = new SimpleDateFormat("yyyyMMddhhmmssS");
		//������ͨ��������ɣ���ȷ�����뱣֤�����ŵ�Ψһ��
		order.setCode(format.format(d));
		String sql="Insert into MyOrder(address,amount,code,phone,remark,status,userid) values('"+order.getAddress()+"',"+order.getAmount()+",'"+order.getCode()+"','"+order.getPhone()+"','"+order.getRemark()+"',"+order.getStatus()+","+order.getUserId()+")";
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
		
	}
	//�޸Ķ���
	public boolean updateOrder(Order order)
	{
		Order oldOrder=getOrder(order.getId());
		
		if(oldOrder==null)
		{
			return false;
		}
		
		String sql="Update MyOrder set address='"+order.getAddress()+"',amount="+order.getAmount()+",code='"+order.getCode()+"',phone='"+order.getPhone()+"',remark='"+order.getRemark()+"',status="+order.getStatus()+",userid="+order.getUserId()+" where id="+order.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
	//ɾ��
	public  boolean deleteOrder(Order order)
	{
		Order oldOrder=getOrder(order.getId());
		
		if(oldOrder==null)
		{
			return false;
		}
		
		String sql="Delete from MyOrder where id="+order.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
}
